package com.hsj.busmanager.service;

import com.hsj.busmanager.api.Bus;
import com.hsj.busmanager.api.Rent;
import com.hsj.busmanager.mapper.RentMapper;
import com.hsj.busmanager.util.CommonUtil;
import com.hsj.busmanager.util.Page;
import com.hsj.busmanager.util.TabelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RentSerivce {

    @Autowired
    private RentMapper rentMapper;

    public void updateIsPay(Integer id) {
        Rent rent = new Rent();
        rent.setId(id);
        rent.setIsPay(1);
        rentMapper.updateByPrimaryKeySelective(rent);
    }

    public void insert(Rent rent) {
        rentMapper.insert(rent);
    }

    public TabelData<Bus> getTotalData(Page page) {

        TabelData tabelData = new TabelData();

        Integer start = (page.getPage()-1)*page.getLimit();

        List<Rent> rentList = rentMapper.getRentTotal(start,page.getLimit());

        if(CommonUtil.listNotNull(rentList)){
            for(Rent rent:rentList){
                rent.setShowCreateTime(CommonUtil.getDateByFormat(rent.getPayTime(),"yyyy-MM-dd"));
            }
        }

        int count = rentMapper.getTotalCount();

        if (rentList == null && rentList.size() == 0) {
            return null;
        }

        tabelData.setCount(count);
        tabelData.setData(rentList);

        return tabelData;


    }
}
